---
title: 'Section 6: Candidate Choice Experiments'
author: "Milan Svolik"
date: "4/20/2023"
output:
  pdf_document: default
  html_document:
    df_print: paged
---

```{r setup, include=FALSE}
rm(list = ls(all = TRUE))
library(tidyverse)
library(stargazer)
library(lmtest)
library(reshape2)
library(foreign)
library(ggplot2)
library(broom)
library(estimatr)
library(texreg)


set.seed(5877)
```

# Figure 6.8
* The distribution of ratings from the ``democracy around the world'' battery for the two experimental undemocratic positions and the item that mirrors the AKP's attempt to overturn its defeat in Istanbul 
```{r}
# Load the 2019 experiment data
rm(list = ls(all = TRUE))
load("data/df_experiments.RData")

# The government fired state employees who expressed support for an opposition party
hist_plot <- df_experiments %>% 
  select(AWB_fired)

hist_plot %>%
  ggplot(aes(x=AWB_fired)) +
    geom_histogram(aes(y=..count../sum(..count..)), binwidth = 1, color="black", fill="white") +
    xlab("The government fired state employees who expressed support for an opposition party") +
    ylab("Fraction") +
    theme_minimal()

ggsave("figures, appendix/t_fired_2019.pdf", width = 10, height = 6, units = "in", dpi = 600)

# The president refused to implement a ruling by the country’s highest court
hist_plot <- df_experiments %>% 
  select(AWB_ruling)

hist_plot %>%
  ggplot(aes(x=AWB_ruling)) +
    geom_histogram(aes(y=..count../sum(..count..)), binwidth = 1, color="black", fill="white") +
    xlab("The president refused to implement a ruling by the country’s highest court") +
    ylab("Fraction") +
    theme_minimal()

ggsave("figures, appendix/t_ruling_2019.pdf", width = 10, height = 6, units = "in", dpi = 600)

# The incumbent president refused to step down from office after a narrow election defeat
hist_plot <- df_experiments %>% 
  select(AWB_stepdown)

hist_plot %>%
  ggplot(aes(x=AWB_stepdown)) +
    geom_histogram(aes(y=..count../sum(..count..)), binwidth = 1, color="black", fill="white") +
    xlab("The incumbent president refused to step down from office after a narrow election defeat") +
    ylab("Fraction") +
    theme_minimal()

ggsave("figures, appendix/t_stepdown_2019.pdf", width = 10, height = 6, units = "in", dpi = 600)
```
# Figures 6.9-6.13
* Vote shares: Heterogeneity
```{R}
rm(list = ls(all = TRUE))
load("data/df_experiments.RData")
df_heterogeneity <- df_experiments
```

### PLOT FUNCTIONS
* plot_shares, plot_cates
```{R}
plot_shares <- function() {

cates <- df_heterogeneity %>% 
  mutate(vote_two=vote_1/(vote_1 + vote_2)) %>% 
  mutate(cond_var = cond_var)

n_subgroups <- nlevels(cates$cond_var)
l_subgroups <- levels(cates$cond_var)
x_subgroups <- factor(l_subgroups, ordered=TRUE, levels=plot_levels)

# COMPUTE
est_two <- tapply(cates$vote_two, list(cates$cond_var, cates$treat), t.test)
est_two <- matrix(as.numeric(unlist(est_two)), ncol=11, byrow=TRUE)
est_two <- est_two[, c(6,4,5)]
colnames(est_two) <- c("mean", "ci_lower", "ci_upper")
est_two <- data.frame(est_two, cond_var=x_subgroups, treat=c(rep(0,n_subgroups) ,rep(1,n_subgroups)), outcome=c("AKP Two Party Vote Share"))

est_0 <- tapply(cates$vote_0, list(cates$cond_var, cates$treat), t.test)
est_0 <- matrix(as.numeric(unlist(est_0)), ncol=11, byrow=TRUE)
est_0 <- est_0[, c(6,4,5)]
colnames(est_0) <- c("mean", "ci_lower", "ci_upper")
est_0 <- data.frame(est_0, cond_var=x_subgroups, treat=c(rep(0,n_subgroups) ,rep(1,n_subgroups)), outcome=c("Abstain"))

est_1 <- tapply(cates$vote_1, list(cates$cond_var, cates$treat), t.test)
est_1 <- matrix(as.numeric(unlist(est_1)), ncol=11, byrow=TRUE)
est_1 <- est_1[, c(6,4,5)]
colnames(est_1) <- c("mean", "ci_lower", "ci_upper")
est_1 <- data.frame(est_1, cond_var=x_subgroups, treat=c(rep(0,n_subgroups) ,rep(1,n_subgroups)), outcome=c("Vote AKP"))

est_2 <- tapply(cates$vote_2, list(cates$cond_var, cates$treat), t.test)
est_2 <- matrix(as.numeric(unlist(est_2)), ncol=11, byrow=TRUE)
est_2 <- est_2[, c(6,4,5)]
colnames(est_2) <- c("mean", "ci_lower", "ci_upper")
est_2 <- data.frame(est_2, cond_var=x_subgroups, treat=c(rep(0,n_subgroups) ,rep(1,n_subgroups)), outcome=c("Vote CHP"))

est_plot <- rbind(est_two, est_1, est_2, est_0)
est_plot <- est_plot %>% 
  mutate(treat=as.factor(treat)) %>%
  mutate(outcome=factor(outcome, levels = c("AKP Two Party Vote Share", "Vote AKP", "Vote CHP", "Abstain")))
 
ggplot(data=est_plot, aes(x=cond_var, y=mean, col=treat, group = treat)) +
  coord_cartesian(ylim = c(0, 1)) +
  geom_hline(yintercept=0) +
  geom_hline(yintercept=0.5, col="grey50") +
  geom_hline(yintercept=1) +
  geom_line(aes(col=treat, group=treat, linetype = treat),  size=1) + 
  geom_point(aes(col=treat, shape=treat), size=2) + 
  geom_errorbar(aes(ymin=ci_lower, ymax=ci_upper, col=treat, group=treat), width=0.05) +
  scale_shape_manual(name  ="", values=c(17, 16), labels=c("     AKP v. CHP","D- AKP v. CHP")) +
  scale_linetype_manual(name  ="", values=c("solid", "dashed"), labels=c("     AKP v. CHP","D- AKP v. CHP")) +
  scale_color_manual(name  ="", values=c("black", "blue"),  labels=c("     AKP v. CHP","D- AKP v. CHP")) +
  scale_x_discrete(name =plot_xaxis, labels=plot_labels) + 
  labs(y = "Fraction",
       title = plot_title
  ) + 
  theme_minimal() +
  facet_wrap(~outcome)
}

plot_cates <- function() {
  
cates <- df_heterogeneity %>% 
  mutate(vote_two=vote_1/(vote_1 + vote_2)) %>% 
  mutate(cond_var = cond_var)

n_subgroups <- nlevels(cates$cond_var)
l_subgroups <- levels(cates$cond_var)
x_subgroups <- factor(l_subgroups, ordered=TRUE, levels=plot_levels)

# COMPUTE
est_two <- c()
est_0 <- c()
est_1 <- c()
est_2 <- c()
for (i in 1:n_subgroups) {
  data_subgroups <- cates %>% filter(cond_var==l_subgroups[i])
  est_two <- rbind(est_two, as.numeric(unlist(difference_in_means(vote_two ~ treat, data_subgroups))))
  est_0 <- rbind(est_0, as.numeric(unlist(difference_in_means(vote_0 ~ treat, data_subgroups))))
  est_1 <- rbind(est_1, as.numeric(unlist(difference_in_means(vote_1 ~ treat, data_subgroups))))
  est_2 <- rbind(est_2, as.numeric(unlist(difference_in_means(vote_2 ~ treat, data_subgroups))))
}

est_two <- est_two[,c(1,7,8)]
colnames(est_two) <- c("mean", "ci_lower", "ci_upper")
est_two <- data.frame(est_two, cond_var=x_subgroups, outcome=c("AKP Two-Party Vote Share"))

est_0 <- est_0[,c(1,7,8)]
colnames(est_0) <- c("mean", "ci_lower", "ci_upper")
est_0 <- data.frame(est_0, cond_var=x_subgroups, outcome=c("Abstain"))

est_1 <- est_1[,c(1,7,8)]
colnames(est_1) <- c("mean", "ci_lower", "ci_upper")
est_1 <- data.frame(est_1, cond_var=x_subgroups, outcome=c("Vote AKP"))

est_2 <- est_2[,c(1,7,8)]
colnames(est_2) <- c("mean", "ci_lower", "ci_upper")
est_2 <- data.frame(est_2, cond_var=x_subgroups, outcome=c("Vote CHP"))

est_plot <- rbind(est_two, est_1, est_2, est_0)
est_plot <- est_plot %>% 
  mutate(outcome=factor(outcome, levels = c("AKP Two-Party Vote Share", "Vote AKP", "Vote CHP", "Abstain")))

ggplot(data=est_plot, aes(x=cond_var, y=mean, group = 1)) +
  coord_cartesian(ylim = c(-0.5, .5)) +
  geom_hline(yintercept=0) +
  theme_minimal() +
  geom_point(size=2, col="blue") + 
  geom_line(size=1, col="blue", linetype="dashed") + 
  geom_errorbar(aes(ymin=ci_lower, ymax=ci_upper), width=0.05, col="blue") +
  scale_x_discrete(name =plot_xaxis, labels=plot_labels) + 
  labs(y = "Change",
       title = plot_title
  ) + 
  theme_minimal() +
  facet_wrap(~outcome)
}
```

### VOTE SHARES
* AGE, STUDENT, UNEMPLOYED, STATE EMPLPYED, EMPLOYMENT TYPE, WEALTH, WORSE OF ECONOMICALLY,  RELIGIOSITY, KURDISH, INTEREST IN POLITICS
```{r}
# AGE
cond_var <- df_heterogeneity$age_cat
plot_title <- c("Age")
plot_xaxis <- c("Age")
plot_levels <- c("18-30","31-40","41-50", "51+")
plot_labels <- c("18-30","31-40","41-50", "51+")
plot_shares()
ggsave("figures, appendix/plot_shares_age.pdf", width = 9, height = 6, units = "in")

# STUDENT
cond_var <- df_heterogeneity$student
plot_title <- c("Student")
plot_xaxis <- c("Student")
plot_levels <- c("No", "Yes")
plot_labels <- c("No", "Yes")
plot_shares()
ggsave("figures, appendix/plot_shares_student.pdf", width = 9, height = 6, units = "in")

# UNEMPLOYED
cond_var <- df_heterogeneity$unemployed
plot_title <- c("Unemployed")
plot_xaxis <- c("Unemployed")
plot_levels <- c("No", "Yes")
plot_labels <- c("No", "Yes")
plot_shares()
ggsave("figures, appendix/plot_shares_unemployed.pdf", width = 9, height = 6, units = "in")

# STATE EMPLOYED 
cond_var <- df_heterogeneity$state_emp
plot_title <- c("State Employed")
plot_xaxis <- c("State Employed")
plot_levels <- c("No", "Yes")
plot_labels <- c("No", "Yes")
plot_shares()
ggsave("figures, appendix/plot_shares_state_emp.pdf", width = 9, height = 6, units = "in")

# WEALTH
cond_var <- df_heterogeneity$wealth_cat 
plot_title <- c("Wealth")
plot_xaxis <- c("Wealth")
plot_levels <- c("Low","Lower middle","Upper middle", "High")
plot_labels <- c("Low","Lower middle","Upper middle", "High")
plot_shares()
ggsave("figures, appendix/plot_shares_wealth.pdf", width = 9, height = 6, units = "in")

# WORSE OFF ECONOMICALLY
cond_var <- df_heterogeneity$worseoff
plot_title <- c("Personal Finances Compared to Year Ago ")
plot_xaxis <- c("How doing financially compared than year ago?")
plot_levels <- c("Much\n worse", "Worse", "Same", "Better", "Much\n better")
plot_labels <- c("Much\n worse", "Worse", "Same", "Better", "Much\n better")
plot_shares()
ggsave("figures, appendix/plot_shares_worseoff.pdf", width = 9, height = 6, units = "in")

# RELIGIOUS
cond_var <- df_heterogeneity$religious
plot_title <- c("Religiosity")
plot_xaxis <- c("How often do you pray?")
plot_levels <- c("Never", "Monthly or \n Holidays", "Weekly", "Everyday")
plot_labels <- c("Never", "Monthly or \n Holidays", "Weekly", "Everyday")
plot_shares()
ggsave("figures, appendix/plot_shares_religious.pdf", width = 9, height = 6, units = "in")

# KURDISH
cond_var <- df_heterogeneity$kurdish
plot_title <- c("Kurdish")
plot_xaxis <- c("Do you speak Kurdish?")
plot_levels <- c("No", "Yes")
plot_labels <- c("No", "Yes")
plot_shares()
ggsave("figures, appendix/plot_shares_kurdish.pdf", width = 9, height = 6, units = "in")

# INTEREST IN POLITICS
cond_var <- df_heterogeneity$interest
plot_title <- c("Interest in Politics")
plot_xaxis <- c("How much do you pay attention to politics?")
plot_levels <- c("None", "Little", "Some", "A lot", "Very much")
plot_labels <- c("None", "Little", "Some", "A lot", "Very much")
plot_shares()
ggsave("figures, appendix/plot_shares_interest.pdf", width = 9, height = 6, units = "in")
```

# Figures 6.14-6.18
* Vote shiftss: Heterogeneity
* AGE, STUDENT, UNEMPLOYED, STATE EMPLPYED, EMPLOYMENT TYPE, WEALTH, WORSE OF ECONOMICALLY,  RELIGIOSITY, KURDISH, INTEREST IN POLITICS
```{r}
# AGE
cond_var <- df_heterogeneity$age_cat
plot_title <- c("Age")
plot_xaxis <- c("Age")
plot_levels <- c("18-30","31-40","41-50", "51+")
plot_labels <- c("18-30","31-40","41-50", "51+")
plot_cates()
ggsave("figures, appendix/plot_cates_age.pdf", width = 9, height = 6, units = "in")

# STUDENT
cond_var <- df_heterogeneity$student
plot_title <- c("Student")
plot_xaxis <- c("Student")
plot_levels <- c("No", "Yes")
plot_labels <- c("No", "Yes")
plot_cates()
ggsave("figures, appendix/plot_cates_student.pdf", width = 9, height = 6, units = "in")

# UNEMPLOYED
cond_var <- df_heterogeneity$unemployed
plot_title <- c("Unemployed")
plot_xaxis <- c("Unemployed")
plot_levels <- c("No", "Yes")
plot_labels <- c("No", "Yes")
plot_cates()
ggsave("figures, appendix/plot_cates_unemployed.pdf", width = 9, height = 6, units = "in")

# STATE EMPLOYED 
cond_var <- df_heterogeneity$state_emp
plot_title <- c("State Employed")
plot_xaxis <- c("State Employed")
plot_levels <- c("No", "Yes")
plot_labels <- c("No", "Yes")
plot_cates()
ggsave("figures, appendix/plot_cates_state_emp.pdf", width = 9, height = 6, units = "in")

# WEALTH
cond_var <- df_heterogeneity$wealth_cat 
plot_title <- c("Wealth")
plot_xaxis <- c("Wealth")
plot_levels <- c("Low","Lower middle","Upper middle", "High")
plot_labels <- c("Low","Lower middle","Upper middle", "High")
plot_cates()
ggsave("figures, appendix/plot_cates_wealth.pdf", width = 9, height = 6, units = "in")

# WORSE OFF ECONOMICALLY
cond_var <- df_heterogeneity$worseoff
plot_title <- c("Personal Finances Compared to Year Ago ")
plot_xaxis <- c("How doing financially compared than year ago?")
plot_levels <- c("Much\n worse", "Worse", "Same", "Better", "Much\n better")
plot_labels <- c("Much\n worse", "Worse", "Same", "Better", "Much\n better")
plot_cates()
ggsave("figures, appendix/plot_cates_worseoff.pdf", width = 9, height = 6, units = "in")

# RELIGIOUS
cond_var <- df_heterogeneity$religious
plot_title <- c("Religiosity")
plot_xaxis <- c("How often do you pray?")
plot_levels <- c("Never", "Monthly or \n Holidays", "Weekly", "Everyday")
plot_labels <- c("Never", "Monthly or \n Holidays", "Weekly", "Everyday")
plot_cates()
ggsave("figures, appendix/plot_cates_religious.pdf", width = 9, height = 6, units = "in")

# KURDISH
cond_var <- df_heterogeneity$kurdish
plot_title <- c("Kurdish")
plot_xaxis <- c("Do you speak Kurdish?")
plot_levels <- c("No", "Yes")
plot_labels <- c("No", "Yes")
plot_cates()
ggsave("figures, appendix/plot_cates_kurdish.pdf", width = 9, height = 6, units = "in")

# INTEREST IN POLITICS
cond_var <- df_heterogeneity$interest
plot_title <- c("Interest in Politics")
plot_xaxis <- c("How much do you pay attention to politics?")
plot_levels <- c("None", "Little", "Some", "A lot", "Very much")
plot_labels <- c("None", "Little", "Some", "A lot", "Very much")
plot_cates()
ggsave("figures, appendix/plot_cates_interest.pdf", width = 9, height = 6, units = "in")
```

# Table 6.4
* Candidate-Choice Experiments, 2018: Vote for the AKP, CHP, and abstention by the respondents' intended vote in the 2018 parliamentary election

### Load the 2019 experiment data
```{R}
rm(list = ls(all = TRUE))
load("data/df_experiments_2018.RData")
```

### AGGREGATE ANALYSIS OF EXPERIMENT 3: AKP v. CHP, policy on head covering, and D- on judges
* Conditions: Candidate 1 - Candidate 2
Condition	Age	Gender Profession Experience Party Covering	D-
1. A	53-37	MM	L-D 15-7 AKP-CHP Allow-Ban D- v N/A	
2. B	53-37	MM	L-D 15-7 AKP-CHP Allow-Ban N/A v D-
3. C	53-37	MM	L-D 15-7 AKP-AKP Allow-Allow D- v N/A	
4. D	53-37	MM	L-D 15-7 AKP-CHP Allow-Ban  N/A v N/A	

### Calculate the share of subjects in each outcome and treatment condition
```{R}
df_experiments_2018 %>%
  select(rid, district, weight, treat, vote_1, vote_2, vote_0) %>%
  filter(!is.na(treat)) %>%
  mutate(vote_share = vote_1/(vote_1 + vote_2)) %>%
  pivot_longer(cols = starts_with("vote_"), names_to = "vote", values_to = "choice") %>%
  group_by(treat, vote) %>% 
  do(lm_robust(choice ~ 1, data = ., clusters=district, weight=weight) %>% tidy) %>%
  arrange(vote, treat) %>%
  select(treat, vote, estimate, conf.low, conf.high) %>%
  mutate(across(estimate:conf.high, ~ .x*100)) %>%
  mutate(across(estimate:conf.high, ~ round(.x, 2)))
```

### Calculate the difference in each outcome between the two treatment conditions 
```{R}
df_experiments_2018 %>%
  select(rid, district, weight, treat, vote_1, vote_2, vote_0) %>%
  filter(!is.na(treat)) %>%
  mutate(vote_share = vote_1/(vote_1 + vote_2)) %>%
  pivot_longer(cols = starts_with("vote_"), names_to = "vote", values_to = "choice") %>%
  group_by(vote) %>% 
  #do(lm_robust(choice ~ 1 + treat, data = .) %>% tidy) %>%
  do(lm_robust(choice ~ 1 + treat, data = ., clusters=district, weight=weight) %>% tidy) %>%
  arrange(vote) %>%
  select(vote, term, estimate, conf.low, conf.high) %>%
  filter(term=="treat") %>%
  mutate(across(estimate:conf.high, ~ .x*100)) %>%
  mutate(across(estimate:conf.high, ~ round(.x, 2)))
```

# Figure 6.19
* Candidate-Choice Experiments, 2018: Vote for the AKP, CHP, and abstention by the respondents' intended vote in the 2018 parliamentary election
```{R}
#Calculate the share of subjects in each outcome and treatment condition
df_plot <- df_experiments_2018 %>%
  filter(!is.na(intent_2018)) %>%
  select(rid, district, weight, treat, vote_1, vote_2, vote_0, intent_2018) %>%
  filter(!is.na(treat)) %>%
  pivot_longer(cols = starts_with("vote_"), names_to = "vote", values_to = "choice") %>%
  group_by(intent_2018, treat, vote) %>% 
  do(lm_robust(choice ~ 1, data = ., clusters=district, weight=weight) %>% tidy) %>%
  arrange(vote, treat, intent_2018) %>%
  select(treat, vote, intent_2018, estimate, conf.low, conf.high)
```

### PLOT THE AKP VOTE
```{r}
plot_legend_labels <- c("     AKP v. CHP","D- AKP v. CHP")
plot_x_label <- c("Intended vote in the 2018 parliamentary election")

df_plot %>% 
  filter(vote=="vote_1") %>%
  mutate(treat=as.factor(treat)) %>%
  ggplot(aes(x=intent_2018, y=estimate, col=treat)) +
  coord_cartesian(ylim = c(0, 1)) +
  geom_hline(yintercept=0) +
  geom_hline(yintercept=0.5, col="grey50") +
  geom_hline(yintercept=1) +
  theme_minimal() +
  geom_line(aes(col=treat, group=treat, linetype = treat),  size=1) + 
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high, col=treat, group=treat), width=0.1) +
  geom_point(aes(col=treat, shape=treat), size=2) +
  scale_shape_manual(name  ="", values=c(17, 16), labels=plot_legend_labels) +
  scale_linetype_manual(name  ="", values=c("solid", "dashed"), labels=plot_legend_labels) +
  scale_color_manual(name  ="", values=c("black", "blue"), labels=plot_legend_labels) +
  labs(
       x = plot_x_label, y = "Fraction",
       title = "Vote AKP"
    ) 

# SAVE THE PLOT
ggsave("figures, appendix//exp_3_vote_1_AKP_favor, 2018.pdf", width = 10, height = 6, units = "in", dpi = 600)
```
### PLOT THE CHP VOTE
```{r}
df_plot %>% 
  filter(vote=="vote_2") %>%
  mutate(treat=as.factor(treat)) %>%
  ggplot(aes(x=intent_2018, y=estimate, col=treat)) +
  coord_cartesian(ylim = c(0, 1)) +
  geom_hline(yintercept=0) +
  geom_hline(yintercept=0.5, col="grey50") +
  geom_hline(yintercept=1) +
  theme_minimal() +
  geom_line(aes(col=treat, group=treat, linetype = treat),  size=1) + 
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high, col=treat, group=treat), width=0.1) +
  geom_point(aes(col=treat, shape=treat), size=2) +
  scale_shape_manual(name  ="", values=c(17, 16), labels=plot_legend_labels) +
  scale_linetype_manual(name  ="", values=c("solid", "dashed"), labels=plot_legend_labels) +
  scale_color_manual(name  ="", values=c("black", "blue"), labels=plot_legend_labels) +
  labs(
       x = plot_x_label, y = "Fraction",
       title = "Vote CHP"
    ) 


# SAVE THE PLOT
ggsave("figures, appendix//exp_3_vote_2_AKP_favor, 2018.pdf", width = 10, height = 6, units = "in", dpi = 600)
```

### PLOT ABSTENTIONS
```{r}
df_plot %>% 
  filter(vote=="vote_0") %>%
  mutate(treat=as.factor(treat)) %>%
  ggplot(aes(x=intent_2018, y=estimate, col=treat)) +
  coord_cartesian(ylim = c(0, 1)) +
  geom_hline(yintercept=0) +
  geom_hline(yintercept=0.5, col="grey50") +
  geom_hline(yintercept=1) +
  theme_minimal() +
  geom_line(aes(col=treat, group=treat, linetype = treat),  size=1) + 
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high, col=treat, group=treat), width=0.1) +
  geom_point(aes(col=treat, shape=treat), size=2) +
  scale_shape_manual(name  ="", values=c(17, 16), labels=plot_legend_labels) +
  scale_linetype_manual(name  ="", values=c("solid", "dashed"), labels=plot_legend_labels) +
  scale_color_manual(name  ="", values=c("black", "blue"), labels=plot_legend_labels) +
  labs(
       x = plot_x_label, y = "Fraction",
       title = "Abstain"
    ) 

# SAVE THE PLOT
ggsave("figures, appendix//exp_3_vote_0_AKP_favor, 2018.pdf", width = 10, height = 6, units = "in", dpi = 600)
```

# Figures 6.20-6.49
* Vote shares: Heterogeneity
```{R}
rm(list = ls(all = TRUE))
load("data/df_experiments_2018.RData")
df_heterogeneity <- df_experiments_2018
```

### PLOT FUNCTIONS
* plot_shares, plot_cates
```{R}
plot_shares <- function() {

cates <- df_heterogeneity %>% 
  mutate(vote_two=vote_1/(vote_1 + vote_2)) %>% 
  mutate(cond_var = cond_var)

n_subgroups <- nlevels(cates$cond_var)
l_subgroups <- levels(cates$cond_var)
x_subgroups <- factor(l_subgroups, ordered=TRUE, levels=plot_levels)

# COMPUTE
est_two <- tapply(cates$vote_two, list(cates$cond_var, cates$treat), t.test)
est_two <- matrix(as.numeric(unlist(est_two)), ncol=11, byrow=TRUE)
est_two <- est_two[, c(6,4,5)]
colnames(est_two) <- c("mean", "ci_lower", "ci_upper")
est_two <- data.frame(est_two, cond_var=x_subgroups, treat=c(rep(0,n_subgroups) ,rep(1,n_subgroups)), outcome=c("AKP Vote Share"))

est_0 <- tapply(cates$vote_0, list(cates$cond_var, cates$treat), t.test)
est_0 <- matrix(as.numeric(unlist(est_0)), ncol=11, byrow=TRUE)
est_0 <- est_0[, c(6,4,5)]
colnames(est_0) <- c("mean", "ci_lower", "ci_upper")
est_0 <- data.frame(est_0, cond_var=x_subgroups, treat=c(rep(0,n_subgroups) ,rep(1,n_subgroups)), outcome=c("Abstain"))

est_1 <- tapply(cates$vote_1, list(cates$cond_var, cates$treat), t.test)
est_1 <- matrix(as.numeric(unlist(est_1)), ncol=11, byrow=TRUE)
est_1 <- est_1[, c(6,4,5)]
colnames(est_1) <- c("mean", "ci_lower", "ci_upper")
est_1 <- data.frame(est_1, cond_var=x_subgroups, treat=c(rep(0,n_subgroups) ,rep(1,n_subgroups)), outcome=c("Vote AKP"))

est_2 <- tapply(cates$vote_2, list(cates$cond_var, cates$treat), t.test)
est_2 <- matrix(as.numeric(unlist(est_2)), ncol=11, byrow=TRUE)
est_2 <- est_2[, c(6,4,5)]
colnames(est_2) <- c("mean", "ci_lower", "ci_upper")
est_2 <- data.frame(est_2, cond_var=x_subgroups, treat=c(rep(0,n_subgroups) ,rep(1,n_subgroups)), outcome=c("Vote CHP"))

est_plot <- rbind(est_two, est_1, est_2, est_0)
est_plot <- est_plot %>% 
  mutate(treat=as.factor(treat)) %>%
  mutate(outcome=factor(outcome, levels = c("AKP Vote Share", "Vote AKP", "Vote CHP", "Abstain")))
 
ggplot(data=est_plot, aes(x=cond_var, y=mean, col=treat, group = treat)) +
  coord_cartesian(ylim = c(0, 1)) +
  geom_hline(yintercept=0) +
  geom_hline(yintercept=0.5, col="grey50") +
  geom_hline(yintercept=1) +
  geom_line(aes(col=treat, group=treat, linetype = treat),  size=1) + 
  geom_point(aes(col=treat, shape=treat), size=2) + 
  geom_errorbar(aes(ymin=ci_lower, ymax=ci_upper, col=treat, group=treat), width=0.05) +
  scale_shape_manual(name  ="", values=c(17, 16), labels=c("     AKP v. CHP","D- AKP v. CHP")) +
  scale_linetype_manual(name  ="", values=c("solid", "dashed"), labels=c("     AKP v. CHP","D- AKP v. CHP")) +
  scale_color_manual(name  ="", values=c("black", "blue"),  labels=c("     AKP v. CHP","D- AKP v. CHP")) +
  scale_x_discrete(name =plot_xaxis, labels=plot_labels) + 
  labs(y = "Fraction",
       title = plot_title
  ) + 
  theme_minimal() +
  facet_wrap(~outcome)
}

plot_cates <- function() {
  
cates <- df_heterogeneity %>% 
  mutate(vote_two=vote_1/(vote_1 + vote_2)) %>% 
  mutate(cond_var = cond_var)

n_subgroups <- nlevels(cates$cond_var)
l_subgroups <- levels(cates$cond_var)
x_subgroups <- factor(l_subgroups, ordered=TRUE, levels=plot_levels)

# COMPUTE
est_two <- c()
est_0 <- c()
est_1 <- c()
est_2 <- c()
for (i in 1:n_subgroups) {
  data_subgroups <- cates %>% filter(cond_var==l_subgroups[i])
  est_two <- rbind(est_two, as.numeric(unlist(difference_in_means(vote_two ~ treat, data_subgroups))))
  est_0 <- rbind(est_0, as.numeric(unlist(difference_in_means(vote_0 ~ treat, data_subgroups))))
  est_1 <- rbind(est_1, as.numeric(unlist(difference_in_means(vote_1 ~ treat, data_subgroups))))
  est_2 <- rbind(est_2, as.numeric(unlist(difference_in_means(vote_2 ~ treat, data_subgroups))))
}

est_two <- est_two[,c(1,7,8)]
colnames(est_two) <- c("mean", "ci_lower", "ci_upper")
est_two <- data.frame(est_two, cond_var=x_subgroups, outcome=c("AKP Vote Share"))

est_0 <- est_0[,c(1,7,8)]
colnames(est_0) <- c("mean", "ci_lower", "ci_upper")
est_0 <- data.frame(est_0, cond_var=x_subgroups, outcome=c("Abstain"))

est_1 <- est_1[,c(1,7,8)]
colnames(est_1) <- c("mean", "ci_lower", "ci_upper")
est_1 <- data.frame(est_1, cond_var=x_subgroups, outcome=c("Vote AKP"))

est_2 <- est_2[,c(1,7,8)]
colnames(est_2) <- c("mean", "ci_lower", "ci_upper")
est_2 <- data.frame(est_2, cond_var=x_subgroups, outcome=c("Vote CHP"))

est_plot <- rbind(est_two, est_1, est_2, est_0)
est_plot <- est_plot %>% 
  mutate(outcome=factor(outcome, levels = c("AKP Vote Share", "Vote AKP", "Vote CHP", "Abstain")))

ggplot(data=est_plot, aes(x=cond_var, y=mean, group = 1)) +
  coord_cartesian(ylim = c(-0.5, .5)) +
  geom_hline(yintercept=0) +
  theme_minimal() +
  geom_point(size=2, col="blue") + 
  geom_line(size=1, col="blue", linetype="dashed") + 
  geom_errorbar(aes(ymin=ci_lower, ymax=ci_upper), width=0.05, col="blue") +
  scale_x_discrete(name =plot_xaxis, labels=plot_labels) + 
  labs(y = "Change",
       title = plot_title
  ) + 
  theme_minimal() +
  facet_wrap(~outcome)
}
```

### VOTE SHARES: AGE, EDUCATION, UNEMPLOYED, STUDENT, WEALTH, RELIGIOSITY, KURDISH, SUPPORT FOR DEMOCRACY
```{r}
# AGE
cond_var <- df_heterogeneity$age_cat
plot_title <- c("Age")
plot_xaxis <- c("Age")
plot_levels <- c("18-30","31-40","41-50", "51+")
plot_labels <- c("18-30","31-40","41-50", "51+")
plot_shares()
ggsave("figures, appendix/plot_shares_age_2018.pdf", width = 9, height = 6, units = "in")

# EDUCATION
cond_var <- df_heterogeneity$edu_cat
plot_title <- c("Education")
plot_xaxis <- c("Highest completed education")
plot_levels <- c("Less than high school","High school","College or higher")
plot_labels <- c("Less than \n high school","High \n school","College \n or higher")
plot_shares()
ggsave("figures, appendix/plot_shares_edu_2018.pdf", width = 9, height = 6, units = "in")

# STUDENT
cond_var <- df_heterogeneity$student
plot_title <- c("Student")
plot_xaxis <- c("Student")
plot_levels <- c("No", "Yes")
plot_labels <- c("No", "Yes")
plot_shares()
ggsave("figures, appendix/plot_shares_student_2018.pdf", width = 9, height = 6, units = "in")

# UNEMPLOYED
cond_var <- df_heterogeneity$unemployed
plot_title <- c("Unemployed")
plot_xaxis <- c("Unemployed")
plot_levels <- c("No", "Yes")
plot_labels <- c("No", "Yes")
plot_shares()
ggsave("figures, appendix/plot_shares_unemployed_2018.pdf", width = 9, height = 6, units = "in")

# WEALTH
cond_var <- df_heterogeneity$wealth_cat
plot_title <- c("Wealth")
plot_xaxis <- c("Wealth")
plot_levels <- c("Low","Lower middle","Upper middle", "High")
plot_labels <- c("Low","Lower middle","Upper middle", "High")
plot_shares()
ggsave("figures, appendix/plot_shares_wealth_2018.pdf", width = 9, height = 6, units = "in")

# RELIGIOUS
cond_var <- df_heterogeneity$religious
plot_title <- c("Religiosity")
plot_xaxis <- c("How often do you pray?")
plot_levels <- c("Never", "Monthly or \n Holidays", "Weekly", "Everyday")
plot_labels <- c("Never", "Monthly or \n Holidays", "Weekly", "Everyday")
plot_shares()
ggsave("figures, appendix/plot_shares_religious_2018.pdf", width = 9, height = 6, units = "in")

# KURDISH
cond_var <- df_heterogeneity$kurdish
plot_title <- c("Kurdish")
plot_xaxis <- c("Do you speak Kurdish?")
plot_levels <- c("No", "Yes")
plot_labels <- c("No", "Yes")
plot_shares()
ggsave("figures, appendix/plot_shares_kurdish_2018.pdf", width = 9, height = 6, units = "in")
```
### VOTE SHIFTS
```{r}
# AGE
cond_var <- df_heterogeneity$age_cat
plot_title <- c("Age")
plot_xaxis <- c("Age")
plot_levels <- c("18-30","31-40","41-50", "51+")
plot_labels <- c("18-30","31-40","41-50", "51+")
plot_cates()
ggsave("figures, appendix/plot_cates_age_2018.pdf", width = 9, height = 6, units = "in")

# EDUCATION
cond_var <- df_heterogeneity$edu_cat
plot_title <- c("Education")
plot_xaxis <- c("Highest completed education")
plot_levels <- c("Less than high school","High school","College or higher")
plot_labels <- c("Less than \n high school","High \n school","College \n or higher")
plot_cates()
ggsave("figures, appendix/plot_cates_edu_2018.pdf", width = 9, height = 6, units = "in")

# STUDENT
cond_var <- df_heterogeneity$student
plot_title <- c("Student")
plot_xaxis <- c("Student")
plot_levels <- c("No", "Yes")
plot_labels <- c("No", "Yes")
plot_cates()
ggsave("figures, appendix/plot_cates_student_2018.pdf", width = 9, height = 6, units = "in")

# UNEMPLOYED
cond_var <- df_heterogeneity$unemployed
plot_title <- c("Unemployed")
plot_xaxis <- c("Unemployed")
plot_levels <- c("No", "Yes")
plot_labels <- c("No", "Yes")
plot_cates()
ggsave("figures, appendix/plot_cates_unemployed_2018.pdf", width = 9, height = 6, units = "in")

# WEALTH
cond_var <- df_heterogeneity$wealth_cat
plot_title <- c("Wealth")
plot_xaxis <- c("Wealth")
plot_levels <- c("Low","Lower middle","Upper middle", "High")
plot_labels <- c("Low","Lower middle","Upper middle", "High")
plot_cates()
ggsave("figures, appendix/plot_cates_wealth_2018.pdf", width = 9, height = 6, units = "in")

# RELIGIOUS
cond_var <- df_heterogeneity$religious
plot_title <- c("Religiosity")
plot_xaxis <- c("How often do you pray?")
plot_levels <- c("Never", "Monthly or \n Holidays", "Weekly", "Everyday")
plot_labels <- c("Never", "Monthly or \n Holidays", "Weekly", "Everyday")
plot_cates()
ggsave("figures, appendix/plot_cates_religious_2018.pdf", width = 9, height = 6, units = "in")

# KURDISH
cond_var <- df_heterogeneity$kurdish
plot_title <- c("Kurdish")
plot_xaxis <- c("Do you speak Kurdish?")
plot_levels <- c("No", "Yes")
plot_labels <- c("No", "Yes")
plot_cates()
ggsave("figures, appendix/plot_cates_kurdish_2018.pdf", width = 9, height = 6, units = "in")
```
